﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmazonCommon.Amazon.ECS;
using System.Configuration;
using System.Web.Caching;
using System.Web;

namespace AmazonCommon
{
    public class ProductManager
    {

        public static Items GetItemsForHomePage()
        {
            string cacheKey="GetItemsForHomePage";
            Cache cache=HttpContext.Current.Cache;

            Items result = cache[cacheKey] as Items;

            if (result==null)
            {
                WebPortal portal = PortalHelper.GetCurrentPortal();
                //portal = PortalHelper.GetCurrentPortalFromDB(portalId);
                List<AmazonLocale> locales = PortalHelper.GetPortalLocalesFromDB(portal.PresetID);

                List<SearchIndexByLocale> searchIndices = PortalHelper.GetSearchIndexByLocale(locales[0].ID);
                Random rand = new Random();
                int randomeIndex = rand.Next(0, searchIndices.Count - 1);
                SearchIndexByLocale wantedSearchIndex = searchIndices[randomeIndex];

                //get products for this random index
                result = AmazonHelper.GetProductsForNode(wantedSearchIndex.BrowseNodeAmazonID, wantedSearchIndex.SearchIndex.IndexName, null, null);
                cache.Add(cacheKey, result, null, DateTime.MaxValue, new TimeSpan(1, 0, 0), CacheItemPriority.High, null);
            }                      
            return result;

        }
    }
}
